//
//  XiangQingVC.m
//  MyTest
//
//  Created by zhiyou on 16-4-22.
//  Copyright (c) 2016年 wangyang. All rights reserved.
//

#import "XiangQingVC.h"
#import "XiangQing.h"
#import "XiangQingCell.h"

@interface XiangQingVC () <UITableViewDataSource, UITableViewDelegate, UIWebViewDelegate>
@property (nonatomic, strong) UIView *bgView;
@property (nonatomic, strong) UIWebView *webView;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArr;
@end

@implementation XiangQingVC
- (void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = YES;
    self.navigationController.navigationBarHidden = YES;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    _dataArr = [[NSMutableArray alloc] init];
    _webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, Width, 1)];
    _webView.delegate = self;
    _webView.scrollView.scrollEnabled = NO;
    _webView.scrollView.showsHorizontalScrollIndicator = NO;
    
    [self setBar];
    [self setMenu];
    
    
    
    [self getData];
    
    
}

#pragma mark -- 初始化tableView
- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, Width, Height-64) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        //[_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    }
    return _tableView;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0){
        static NSString *identifier = @"cell";
        XiangQingCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (!cell){
            cell = [[XiangQingCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        }
        XiangQing *xiangQing = _dataArr[0];
        cell.xiangQing = xiangQing;
        return cell;
        
    }else if (indexPath.row == 1){
        static NSString *identifier = @"cell1";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (!cell){
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        }
        cell.textLabel.text = @"商品详情";
        return cell;
        
    } else {
        static NSString *identifier = @"cell2";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (!cell){
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
            [cell.contentView addSubview:_webView];
            /* 忽略点击效果 */
            [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        }
        return cell;
    }

}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        return 400;
    } else if (indexPath.row == 1){
        return 40;
    }else {
        return self.webView.frame.size.height;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 1;
}
#pragma mark -- WebViewDelegate
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    CGFloat height = [[self.webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight"] floatValue];
    _webView.frame = CGRectMake(self.webView.frame.origin.x,self.webView.frame.origin.y, Width, height+100);
    [_tableView reloadData];
}
#pragma mark -- 获取数据
- (void)getData
{
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    __weak XiangQingVC *mySelf = self;
    [manager GET:_URL parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSError *error = nil;
        NSData *data = [NSJSONSerialization dataWithJSONObject:responseObject options:NSJSONWritingPrettyPrinted error:&error];
        //NSLog(@"data %@",[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]);
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
        
        NSDictionary *dic1 = [dic objectForKey:@"data"];
        //NSLog(@"%@",dic1);
        XiangQing *xiangQing = [[XiangQing alloc] initWithDictionary:dic1];
        
        [_dataArr addObject:xiangQing];
        [self.view addSubview:self.tableView];
        [mySelf setBottom];
        NSString *text = [[dic objectForKey:@"data"] objectForKey:@"text"];
        NSString *head = [NSString stringWithFormat:@"<head><style>img{max-width:%fpx !important;}</style></head>",Width];
        NSString *url = [head stringByAppendingString:text];
        NSLog(@"%@",url);
        [mySelf.webView loadHTMLString:url baseURL:nil];
        
        [_tableView reloadData];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
}
#pragma mark -- 底部

- (void)setBottom
{
    UIView *bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, Height-40, Width, 40)];
    [self.view addSubview:bottomView];
    
    UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, Width, 40)];
    imgView.image = [UIImage imageNamed:@"bg_toolbar"];
    [bottomView addSubview:imgView];
    
    XiangQing *xiangQing = _dataArr[0];
    for (int i = 0; i < 3; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(70*i, 0, 70, 40);
        button.tag = i;
        button.titleLabel.font = [UIFont systemFontOfSize:14];
        if (i == 0) {
            [button setTitle:[NSString stringWithFormat:@"赞%@",xiangQing.support] forState:UIControlStateNormal];
        }else if (i == 1) {
            [button setTitle:[NSString stringWithFormat:@"差%@",xiangQing.against] forState:UIControlStateNormal];
        } else {
            [button setTitle:@"转发" forState:UIControlStateNormal];
        }
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(btn:) forControlEvents:UIControlEventTouchUpInside];
        [bottomView addSubview:button];
    }
    UIButton *toBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    toBtn.frame = CGRectMake(220, 5, Width-230, 35);
    toBtn.backgroundColor = [UIColor redColor];
    toBtn.layer.cornerRadius = 5;
    toBtn.clipsToBounds = YES;
    toBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [toBtn setTitle:@"直达链接" forState:UIControlStateNormal];
    [toBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [toBtn addTarget:self action:@selector(toWeb) forControlEvents:UIControlEventTouchUpInside];
    [bottomView addSubview:toBtn];
    
}
- (void)btn:(UIButton *)button
{
    NSLog(@"%ld",(long)button.tag);
}
- (void)toWeb
{
    NSLog(@"直达链接");
}
#pragma mark -- 创建导航
- (void)setBar
{
    UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, Width, 64)];
    bgView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:bgView];
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:bgView.frame];
    imageView.image = [UIImage imageNamed:@"topbar"];
    [bgView addSubview:imageView];
    
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame = CGRectMake(0, 20, 50, 40);
    [backBtn setBackgroundImage:[UIImage imageNamed:@"F_iocn_normal"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    [bgView addSubview:backBtn];
    
    UIButton *menuBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    menuBtn.frame = CGRectZero;
    [menuBtn setBackgroundImage:[UIImage imageNamed:@"D_iocn_normal"] forState:UIControlStateNormal];
    [menuBtn addTarget:self action:@selector(showMenu) forControlEvents:UIControlEventTouchUpInside];
    [bgView addSubview:menuBtn];
    
    [menuBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(bgView.mas_top).offset(20);
        make.right.offset(-10);
        make.width.offset(50);
        make.height.offset(40);
    }];
    
    UILabel *titleLab = [[UILabel alloc] initWithFrame:CGRectZero];
    titleLab.text = @"商品详情";
    titleLab.font = [UIFont systemFontOfSize:20];
    [bgView addSubview:titleLab];
    
    [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(bgView.mas_centerX);
        make.centerY.equalTo(bgView.mas_centerY).offset(10);
    }];
}
- (void)back
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)showMenu
{
    static int count = 1;
    if (count%2 == 1) {
        
        [self showView];
    }else
    {
        [self hideView];
    }
    count ++;
}
- (void)setMenu
{
    _bgView = [[UIView alloc] initWithFrame:CGRectMake(Width*2/3, 64, Width/3, 0)];
    _bgView.backgroundColor = [UIColor grayColor];
    [self.view addSubview:_bgView];
    [self.view bringSubviewToFront:_bgView];
    
    NSArray *arr = @[@"首页",@"我的",@"消息"];
    for (int i = 0; i < 3; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(0, 30*i, Width/3, 30);
        button.tag = i;
        [button setTitle:arr[i] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
        [_bgView addSubview:button];
        _bgView.clipsToBounds = YES;
    }
}
- (void)showView
{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:1];
    _bgView.frame = CGRectMake(Width*2/3, 64, Width/3, 90);
    [UIView commitAnimations];
}
- (void)hideView
{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:1];
    _bgView.frame = CGRectMake(Width*2/3, 64, Width/3, 0);
    [UIView commitAnimations];
}
- (void)clickButton:(UIButton *)button
{
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
